'use strict';

function countSameElements(collection) {
  let result=[];
  for(let i=0;i<collection.length;i++){
    let num=1;
    let object={};
    if(collection[i].length>1){
      num=parseInt(collection[i].replace(/[^0-9]/ig,"")) ;
      collection[i]=collection[i].substring(0,1);
    }
    for(let j=i+1;j<collection.length;j++){
      if(collection[j].length>1){
        if(collection[i]==collection[j].substring(0,1)){
          num=num+parseInt(collection[j].replace(/[^0-9]/ig,""));
          collection.splice(j,1);
          j--;
        }
      }
      else{
        if(collection[i]==collection[j]){
          num++;
          collection.splice(j,1);
          j--;
        }
      }
     }
     object.key=collection[i];
     object.count=num;
     result.push(object);
  }
  console.log(result);
  return result;
}

function includes(collection,ch){
  for(let item of collection){
    if(item===ch){
      return true;
    }
  }
  return false;
}

function createUpdatedCollection(collectionA, objectB) {
  let collectionC=countSameElements(collectionA);
  let collectionB=objectB.value;
  for(let i=0;i<collectionC.length;i++){
    if(includes(collectionB,collectionC[i].key)){
      collectionC[i].count-=Math.floor(collectionC[i].count/3);
    }
  }
  console.log(collectionC);
  return collectionC;
}
module.exports =  createUpdatedCollection;
createUpdatedCollection([
  'a', 'a', 'a',
  'e', 'e', 'e', 'e', 'e', 'e', 'e',
  'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h',
  't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't', 't',
  'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f',
  'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c',
  'g', 'g', 'g', 'g', 'g', 'g', 'g',
  'b', 'b', 'b', 'b', 'b', 'b',
  'd-5'
], {value: ['a', 'd', 'e', 'f']});